var express = require('express');
var router = express.Router();
const cartSchema = require('../module/cart');
const shopsdetailSchema = require('../module/shopsdetail');
const jwt = require('jsonwebtoken');
let secret = 'houduan'

//从购物车添加到添加到未付款,token,id,num
router.post('/nopaid', function (req, res, next) {
    let { token, idlist } = req.body
    try {

        //破解token
        let data = jwt.verify(token, secret)
        let { username } = data

        //循环前端传进的列表
        for (value in idlist) {

            //购物车删掉该货物数量
            shopsdetailSchema.update({ username, 'shops.id': value.id }, { $inc: { 'shops.$.num': -num } })

            //未付款处加入该货物
            shopsdetailSchema.updateOne({ username }, { $push: { nopaidorder: { id: value.id, num: value.num } } })
        }

        //如果购物车物品数量为0，则去掉该商品
        shopsdetailSchema.update({ username, 'shops.num': 0 }, { $pull: { 'shops.$.num': -num } })



    } catch (error) {
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }
})

//从购物车添加到添加到已付款,token,id,num
router.post('/paid', function (req, res, next) {
    let { token, idlist } = req.body
    try {

        //破解token
        let data = jwt.verify(token, secret)
        let { username } = data

        for (value in idlist) {

            //购物车删掉该货物数量
            shopsdetailSchema.update({ username, 'shops.id': value.id }, { $inc: { 'shops.$.num': -num } })

            //未付款处加入该货物
            shopsdetailSchema.updateOne({ username }, { $push: { paidorder: { id: value.id, num: value.num } } })
        }

    } catch (error) {
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }
})

//获得未付款订单详情
router.post('/getnopaid', function (req, res, next) {
    let { token } = req.body
    try {

        //破解token
        let data = jwt.verify(token, secret)
        let { username } = data
        let shopDetailList = []
        let shopInfo = []
        //根据token获得所有购物车数据
        async function getshopIdList() {
            //通过username在购物车列表找到用户
            await cartSchema.findOne({ username }, (err, doc) => {
                if (err) throw err
                shopDetailList = doc.nopaidorder

            })
            console.log("第一步", shopDetailList)
        }

        async function getinfodetail(shopid, shopnum) {
            let obj = {}
            await shopsdetailSchema.findOne({ "list.id": shopid }, (err, doc) => {
                let title = doc.title
                for (value of doc.list) {
                    if (value.id == shopid) {
                        obj.id = shopid;
                        obj.num = shopnum;
                        obj.url = value.url;
                        obj.detail = value.title;
                        obj.price = value.price;
                        obj.title = title
                        shopInfo.push(obj)
                    }
                }
            })
            console.log("第二步", shopInfo)
        }

        async function getinfolist() {
            //获得所有未付款商品的idId
            await getshopIdList()
            console.log("shopDetailList===================", shopDetailList)
            //循环商品id在商品详情页获得商品详情
            for (value of shopDetailList) {
                let shopid = value.id
                let shopnum = value.num
                //获得需要的商品信息
                await getinfodetail(shopid, shopnum)
            }
            console.log("shopInfo===================", shopInfo)
            res.json({
                status: 0,
                msg: "获得成功",
                data: shopInfo
            })
        }
        getinfolist()

    } catch (error) {
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }
})

//获得已付款订单
router.post('/getpaid', function (req, res, next) {
    let { token } = req.body
    try {

        //破解token
        let data = jwt.verify(token, secret)
        let { username } = data
        let shopDetailList = []
        let shopInfo = []
        //根据token获得所有购物车数据

        async function getshopIdList() {
            //通过username在购物车列表找到用户
            await cartSchema.findOne({ username }, (err, doc) => {
                if (err) throw err
                shopDetailList = doc.paidorder

            })
            console.log("第一步", shopDetailList)
        }

        async function getinfodetail(shopid, shopnum) {
            let obj = {}
            await shopsdetailSchema.findOne({ "list.id": shopid }, (err, doc) => {
                let title = doc.title
                for (value of doc.list) {
                    if (value.id == shopid) {
                        obj.id = shopid;
                        obj.num = shopnum;
                        obj.url = value.url;
                        obj.detail = value.title;
                        obj.price = value.price;
                        obj.title = title
                        shopInfo.push(obj)
                    }
                }
            })
            console.log("第二步", shopInfo)
        }

        async function getinfolist() {
            //获得所有付款商品的id
            await getshopIdList()
            console.log("shopDetailList===================", shopDetailList)
            //循环商品id在商品详情页获得商品详情
            for (value of shopDetailList) {
                let shopid = value.id
                let shopnum = value.num
                //获得需要的商品信息
                await getinfodetail(shopid, shopnum)
            }
            console.log("shopInfo===================", shopInfo)
            res.json({
                status: 0,
                msg: "获得成功",
                data: shopInfo
            })
        }
        getinfolist()

    } catch (error) {
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }
})






module.exports = router;